package com.spring.demo5;

import javax.annotation.PreDestroy;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

	/**
	 * Spring注解 setter方法注解
	 * @Required 
	 * @Autowired(required=false)
	 */
	public void testDemo(){
		ApplicationContext context = new ClassPathXmlApplicationContext("bean5.xml");
		Student student = (Student) context.getBean("student");
		Student2 student2 = (Student2) context.getBean("student2");
		System.out.println("Name : " + student.getName() );
		System.out.println("Age : " + student.getAge() );
		System.out.println("====++++====");
		System.out.println("Name : " + student2.getName() );
		System.out.println("Age : " + student2.getAge() );
	}
	/**
	 *  Spring注解 setter方法注解
	 *  @Autowired
	 * 	@Resource(name= "spellChecker")
	 */
	public void testDemo1(){
		ApplicationContext context = new ClassPathXmlApplicationContext("bean5.xml");
		TextEditor te = (TextEditor) context.getBean("textEditor");
		te.spellCheck();
	}
	/**
	 *  Spring注解 属性注解
	 *  @Autowired
	 */
	public void testDemo2(){
		ApplicationContext context = new ClassPathXmlApplicationContext("bean5.xml");
		TextEditor2 te = (TextEditor2) context.getBean("textEditor2");
		te.spellCheck();
	}
	/**
	 *  Spring注解 构造器注解
	 *  @Autowired
	 */
	public void testDemo3(){
		ApplicationContext context = new ClassPathXmlApplicationContext("bean5.xml");
		TextEditor3 te = (TextEditor3) context.getBean("textEditor3");
		te.spellCheck();
	}
	/**
	 * Spring注解 属性注解(手动指定同一类beans中的一个)
	 * @Autowired
	 * @Qualifier("student3")//注释后会注入student Bean
	 */
	public void testDemo4(){
		ApplicationContext context = new ClassPathXmlApplicationContext("bean5.xml");
		Profile profile = (Profile) context.getBean("profile");
		profile.printAge();
		profile.printName();
	}
	/**
	 * Spring注解 指定bean的初始化和销毁回掉函数
	 * @PreDestroy
	 * @PostConstruct
	 */
	public void testDemo5(){
		AbstractApplicationContext context = new ClassPathXmlApplicationContext("bean5.xml");
		HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
		obj.getMessage();
		context.registerShutdownHook();
	}
	/**
	 * Spring基于java的配置
	 * @Configuration
	 * @Bean
	 */
	public void testDemo6(){
		//ApplicationContext ctx = new AnnotationConfigApplicationContext(HelloWorldConfig.class);
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
		ctx.register(HelloWorldConfig.class);
		ctx.refresh();
		HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
		helloWorld.setMessage("Hello World!");
		helloWorld.getMessage();
	}
	/**
	 * Spring基于java的配置(注入bean的依赖性)
	 */
	public void testDemo7(){
		ApplicationContext ctx = new AnnotationConfigApplicationContext(TextEditorConfig.class);
		TextEditor4 te = ctx.getBean(TextEditor4.class);
		te.spellCheck();
	}
	/**
	 * Spring基于java的配置
	 * @Import(ConfigA.class)允许在另一个配置类中加载@bean定义
	 * @Bean(initMethod = "init", destroyMethod = "cleanup" ) 指定A的初始化和销毁方法
	 * @Scope 指定Bean的范围，
	 */
	public void testDemo8(){
		ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigB.class);
		A a = ctx.getBean(A.class);
		A a2 = ctx.getBean(A.class);
		B b = ctx.getBean(B.class);
		System.out.println("a&a2是否是单例"+(a==a2));
	}
	public static void main(String[] args) {
		new MainApp().testDemo();
		System.out.println("===============setter====================");
		new MainApp().testDemo1();
		System.out.println("===============属性====================");
		new MainApp().testDemo2();
		System.out.println("===============构造函数====================");
		new MainApp().testDemo3();
		System.out.println("===============Qualifier====================");
		new MainApp().testDemo4();
		System.out.println("5===================================");
		new MainApp().testDemo5();
		System.out.println("6===================================");
		new MainApp().testDemo6();
		System.out.println("7===================================");
		new MainApp().testDemo7();
		System.out.println("8===================================");
		new MainApp().testDemo8();
	}
}
